import MySQLdb,datetime,struct,hashlib

WEISHU=29
SEG=13
SEG_OFFSET=11
OFFSET_VALUE=5
def get_full_md5(url):
    m=hashlib.md5()
    m.update(url)
    b_m=m.digest()
    return b_m
def md52int(b_m,weishu=WEISHU):
    b_length=len(b_m)
    l_m=0L
    for i in xrange(b_length):
        b_quan=2**(8*i)
        b_int=struct.unpack('B',b_m[i])[0]
        l_m+=b_int*b_quan
    return l_m%(2**weishu)
def get_md52int(url):
    return md52int(get_full_md5(url))

cxn=MySQLdb.connect(db='pachong',host='localhost',user='root',passwd='123456ms')
cxn.set_character_set('utf8')
cur=cxn.cursor()

sql='select min(id),max(id) from csdn'
cur.execute(sql)
min_id,max_id=cur.fetchone()

step=10**5
idx=min_id
record_num=0

while idx<=max_id:
    sql_pref='select * from %s'%'csdn'
    sql=sql_pref+' where id between %s and %s'
    cur.execute(sql,(str(idx),str(idx+step)))
    for record in cur.fetchall():
        _idx=record[0]
        cal_idx=get_md52int(record[1])
        if _idx!=cal_idx:
            print _idx,cal_idx,record[1]
        record_num+=1
    idx+=step

    
print 'end: ',record_num

cxn.commit()

cur.close()

cxn.close()
